#include <SDL/SDL.h>

#include <stdlib.h>
#include <stdio.h>

void resultat(SDL_Surface *ecran, int tab[][3]);
void jouer (SDL_Surface *ecran);

int main(int argc, char *argv[])
{  SDL_Surface *ecran = NULL,*imageDeFond = NULL, *image = NULL;
   SDL_Rect pos;    int i =0, done = 1;
   int idJoueur = 1;
   SDL_Event event;
    SDL_Rect positionFond;
      positionFond.x = 0;
     positionFond.y = 0;

   pos.x = 0;
   pos.y = 0;

SDL_Init(SDL_INIT_VIDEO);
ecran = SDL_SetVideoMode(600, 600, 32, SDL_HWSURFACE);

SDL_WM_SetCaption("Mon degrade en SDL !", NULL);
imageDeFond = SDL_LoadBMP("images/grid.bmp");
while(done){
   SDL_WaitEvent(&event);
   switch(event.type){
   case SDL_QUIT:
       done = 0;
       break;
   case SDL_KEYDOWN:
       switch(event.key.keysym.sym){
       case SDLK_ESCAPE:
           done = 0;
           break;
       case SDLK_j:
           jouer(ecran);
           break;
       }
       break;
   }
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format,0,0,0 ));
SDL_BlitSurface(imageDeFond, NULL, ecran, &positionFond);
SDL_BlitSurface(image, NULL, ecran, &pos);
SDL_Flip(ecran);
  }

 SDL_FreeSurface(image);
 SDL_FreeSurface(imageDeFond);
 SDL_Quit();
return EXIT_SUCCESS;
}

void jouer (SDL_Surface *ecran)
{
 SDL_Surface *fond = NULL, *imageX = NULL, *imageO = NULL;
 SDL_Rect position, positionBlit;
 SDL_Event event;

 int done = 1, idJoueur = 1, i, j;
 int tab[3][3] = {0};
   for(i=0; i<3; i++){
       for(j=0; j<3; j++){
           tab[i][j] = 0;
       }
   }
 fond = SDL_LoadBMP("images/grid.bmp");
 imageO = SDL_LoadBMP("images/o.bmp");
 imageX = SDL_LoadBMP("images/x.bmp");

 while(done){
   SDL_WaitEvent(&event);
   switch(event.type){
       case SDL_QUIT:
           done = 0;
           break;
       case SDL_MOUSEBUTTONDOWN:
           switch(event.button.button){
               case SDL_BUTTON_LEFT:
                   i = event.motion.x/200;
                   j = event.motion.y/200;
                   if(idJoueur == 1){
                       if(tab[i][j] == 0){
                           tab[i][j] = 1;
                           idJoueur = 2;
                       }
                   }else if(idJoueur == 2){
                       if(tab[i][j] == 0){
                           tab[i][j] = 2;
                           idJoueur = 1;
                       }
                   }
                   resultat(ecran, tab);
                   break;
           }
           break;
   }

   SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));

   for(i=0; i<3; i++){
       for(j=0; j<3; j++){
           positionBlit.x = i*200;
           positionBlit.y = j*200;
           switch(tab[i][j]){
               case 0:
                   SDL_BlitSurface(fond, NULL, ecran, &positionBlit);
                   break;
               case 1:
                   SDL_BlitSurface(imageX, NULL, ecran, &positionBlit);
                   break;
               case 2:
                   SDL_BlitSurface(imageO, NULL, ecran, &positionBlit);
                   break;
           }
       }
   }
   SDL_Flip(ecran);
 }

 SDL_FreeSurface(imageX);
 SDL_FreeSurface(imageO);
 SDL_FreeSurface(fond);

}

void resultat(SDL_Surface *ecran, int tab[][3])
{
   SDL_Surface *res = NULL;
   SDL_Rect position;
   SDL_Event event;
   position.x = 0;
   position.y = 0;

   res = SDL_LoadBMP("images/failed.bmp");
   int i, j, s = 0, done = 1;

   for(i=0; i<3; i++){
       for(j=0; j<3; j++){
           if(tab[i][j] != 0)
               s++;
       }
   }

   if(s == 9){
       for(i=0; i<3; i++){
           for(j=0; j<3; j++){
               if((tab[i][j] == tab[i+1][j]) && (tab[i][j] == tab[i+2][j])){
                   res = SDL_LoadBMP("images/winner.bmp");
               }
               else if((tab[i][j] == tab[i+1][j+1]) && (tab[i][j] == tab[i+2][j+2]))
                   res = SDL_LoadBMP("images/winner.bmp");
               else if((tab[i][j] == tab[i][j+1]) && (tab[i][j] == tab[i][j+2]))
                   res = SDL_LoadBMP("images/winner.bmp");
           }
       }

       while(done){
           SDL_PollEvent(&event);
           switch(event.type){
               case SDL_QUIT:
                   done = 0;
                   break;
               case SDL_KEYDOWN:
                   switch(event.key.keysym.sym){
                       case SDLK_ESCAPE:
                           done = 0;
                           break;
                   }
                   break;
           }

           SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
           SDL_BlitSurface(res, NULL, ecran, &position);
           SDL_Flip(ecran);
       }
   SDL_FreeSurface(res);
   }
}
